/**
 * File  : Mutex.cc
 * Time  : 2014-12-17 10:24
 * Author: KashingLee
 * Ref   : mooon
 */
#include "Mutex.h"

Mutex::Mutex() {
  int ret = pthread_mutex_init(&mutex_, NULL);
  if (ret) {
    throw ThreadException(THREAD_MUTEX_INIT);
  }
}

Mutex::~Mutex() {
  pthread_mutex_destroy(&mutex_);
}

void Mutex::Lock() {
  int ret = pthread_mutex_lock(&mutex_);
  if (ret) {
    throw ThreadException(THREAD_LOCK);
  }
}

void Mutex::Unlock() {
  int ret = pthread_mutex_unlock(&mutex_);
  if (ret) {
    throw ThreadException(THREAD_UNLOCK);
  }
}

bool Mutex::TryLock() {
  int ret = pthread_mutex_trylock(&mutex_);
  return ret==0;
}
